UIActivityPrivilege = Class("UIActivityPrivilege", UIComponentBase)

function UIActivityPrivilege:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.banner = self:GetChild("bannerBg")
    self.bannerChar = self:GetChild("bannerChar")
    self.bannerText = self:GetChild("bannerText")
    self.title = self:GetChild("title")
    self.btnBuy1 = self:GetChild("btnBuy1")
    self.btnBuy2 = self:GetChild("btnBuy2")
    self.giftList = self:SetDynamicList(self:GetChild("list"),self.updateDescItem)
    self.tipTypeController = self:GetController("tipType")
    self.typeController = self:GetController("Type")
    self.buyTips = self:GetChild("BuyTips")
end

function UIActivityPrivilege:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self:RemoveAllTimeCall();
    local maxNum = 4;
    if self.itemData[1] ~= nil then
        maxNum = self.itemData[1].f_Num;
    end
    if self.severData.overTime < 0 then
        self.tipTypeController.selectedIndex = 1;
        self.typeController.selectedIndex = 1;
        self.bannerText.text = getLanguage("DailyConsumptionTitle4000103");
        self.countDownNum = 100;
    else
        self.typeController.selectedIndex = 0;
        self.countDownNum = self.severData.overTime - HelperFunc.GetServerTime();
        if self.countDownNum <= 0 then
            self.tipTypeController.selectedIndex = 0;
        else
            self:TimeCall(self.OnCountDown, 1000, -1);
            self.tipTypeController.selectedIndex = 1;
            self:OnCountDown();
        end
        self.buyTips.text = string.format(getLanguage("DailyConsumptionTitle4000104"), maxNum - self.severData.bugCount);
    end
    self.bannerChar.url = tostring(self.modelTableData.f_BannerHead);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    self.title.text = getLanguage(self.modelTableData.f_Title, nil, "");
    self.modelTableData.f_NobleBuff = decodeJsonStrToTabel(self.modelTableData.f_NobleBuff)
    self.giftList.numItems = #self.modelTableData.f_NobleBuff;
    HelperFunc.ListTranstionPlay(self.giftList);
    self:updateItem(self.btnBuy1, self.itemData[1]);
    self:updateItem(self.btnBuy2, self.itemData[2]);
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine)
end

function UIActivityPrivilege:updateDescItem(index, itemObj)
    local tableData = self.modelTableData.f_NobleBuff[index];
    if tableData == nil then
        itemObj.visible = false;
        return ;
    end
    local num = itemObj:GetChild("num");
    local text = itemObj:GetChild("text");
    local stateController = itemObj:GetController("state");
    num.text = index;
    local buff = G.dataTable["t_ecobuff"][tostring(tableData)];
    if buff ~= nil then
        text.text = getLanguage(buff.f_BuffDesc);
    else
        text.text = "";
    end
    if self.countDownNum <= 0 then
        stateController.selectedIndex = 0;
    else
        stateController.selectedIndex = 1;
    end
end

function UIActivityPrivilege:updateItem(itemObj, tableData)
    if tableData == nil then
        itemObj.visible = false;
        return ;
    end
    local title = itemObj:GetChild("title");
    local price = itemObj:GetChild("price");
    title.text = getLanguage(tableData.f_Desc, nil, "");
    price.text = "￥"..tableData.f_Price;
    itemObj.onClick:Clear();
    itemObj.onClick:Add(function() showUI(PanelNames.UIPrivilegeConfirm, nil, self.activeId, tableData.f_ItemId); end);
end

function UIActivityPrivilege:OnCountDown()
    if self.countDownNum <= 0 then
        self.tipTypeController.selectedIndex = 0;
        self:Refresh();
        return true;
    end
    self.bannerText.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), HelperFunc.GetSurpTimeStr(self.countDownNum));
    self.countDownNum = self.countDownNum - 1;
end

function UIActivityPrivilege:onBuySuccess()
end

-- 卸载
function UIActivityPrivilege:OnDestroy()
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
end